package engine.logic.login
{
	import com.greensock.TweenLite;
	
	import engine.data.ModelLocator;
	import engine.logic.ActSee;
	import engine.logic.loading.LoadingLogic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;

	public class CastleSelectUI implements ILoginUI
	{
		public function CastleSelectUI(clip:MovieClip,container:MovieClip)
		{	
			_scrollAnimation=new ScrollAnimation();
			
			_castleSelectUI=clip;
			_castleSelectUI.gotoAndStop(1);
			_container=container;
			
			_castleList=[];
			_castleNums=this.getCastleNums();
			_raceNums=this.getRaceNums();
			
			for(var i:int=0;	i<_castleNums;	i++)
			{
				_castleList.push(getRaceCastles(i));
			}
		}
		
		//-----
		private var _nextBtn:MovieClip;//下一步按钮
		private var _selectRaceMc:MovieClip;//选择种族提示动画
		private var _selectCastleMc:MovieClip;//选择城堡提示动画
		//------
		
		
		private var _completeCallBack:Function;
		
		private var _castleNums:int;
		private var _raceNums:int;
		
		private var _selectedRaceId:int=-1;
		private var _selectedCastleId:int=-1;
		/**城堡选择界面*/
		private var _castleSelectUI:MovieClip;
		private var _container:MovieClip;
		
		private var _scrollAnimation:ScrollAnimation;
		//=============================
		/**
		 *种族可选城堡表 
		 */		
		private var _castleList:Array;

		private var _isCgPlayed:Boolean=false;
		private var _isCastleTipsShowed:Boolean=false;
		
		private var _scrollTxtRect:Rectangle=new Rectangle(0,0,0,0);//17,14
		
		/**默认城堡描述*/
		private var _defaultCastleDis:String;
		/**默认光明种族描述*/
		private var _defaultRace0Dis:String;
		/**默认黑暗种族描述*/
		private var _defaultRace1Dis:String;
		
		public function show(info:Object=null):void
		{
//			if(false 	==	_isCgPlayed)
//			{
////				LoadingLogic.getInstance().setProcess(0,"");
////				SwfLoader.StartLoad(this.getAnimationURL(),null,cgCompleteHandler,[onLoadingHandle])
////				_isCgPlayed=true;
//			}
//			else
//			{
				playCastleUI(init);
//				_container.addChild(_castleSelectUI);
//				init();
//			}
		}
		
		
		/**
		 *初始化
		 */		
		private function init():void
		{	
			_castleSelectUI.stop();
			
			_selectedCastleId	=	-1;
			_selectedRaceId		=	-1;
			_isCastleTipsShowed	=	false;
			
			_nextBtn	=	_castleSelectUI.castleSheet.nextBtn;
			_selectRaceMc=	_castleSelectUI.selectRaceTip;
			_selectCastleMc=_castleSelectUI.selectCastleTip;
			
			_selectRaceMc.gotoAndStop(1);
			_selectCastleMc.gotoAndStop(1);
			
//			getCampMaskMc(0).gotoAndStop(1);
//			getCampMaskMc(1).gotoAndStop(1);
			
			//=========初始化种族Icon
			var raceIcon:MovieClip;
			//			var len:uint=int(ModelLocator.getInstance().);
			for(var i:uint	=0;	i<_raceNums;	i++)
			{
				raceIcon=this.getRaceIcon(i);
				raceIcon.addEventListener(MouseEvent.CLICK,raceIconClickHandler);
			}
			//=========初始化城堡Icon
			var castleIcon:MovieClip;
			for(var j:uint=0;	j<_castleNums;	j++)
			{
				castleIcon=this.getCastleIcon(j);
				castleIcon.addEventListener(MouseEvent.CLICK,castleIconClickHandler);
			}
			
			//=====初始化下一步按钮
			_nextBtn.addEventListener(MouseEvent.CLICK,nextBtnClickHandler);
			_nextBtn.addEventListener(MouseEvent.MOUSE_OVER,nextBtnOverHandler);
			_nextBtn.addEventListener(MouseEvent.MOUSE_OUT,nextBtnOutHandler);
			
			MovieClip(_nextBtn).gotoAndStop(2);
			MovieClip(_nextBtn).buttonMode=true;
			
			_scrollTxtRect.width=	int(getCastleDiscribeTxt().width);
			_scrollTxtRect.height=	int(getCastleDiscribeTxt().height);
			
			_defaultCastleDis	=	getCastleDiscribeTxt().htmlText;
			
			_defaultRace0Dis	=	getRaceDiscribeTxt(0).htmlText;//光明方显示文本,0,1
			
			_defaultRace1Dis	=	getRaceDiscribeTxt(3).htmlText;//黑暗方显示文本,2,3
			
			resetRaceIcons();
			resetCastleIcons();
			
			
			_selectRaceMc.gotoAndPlay(2);
//			_castleSelectUI.castleSheet.y=620;
			
//			this.getCastleDiscribeTxt().x=_scrollTxtRect.x;
//			this.getCastleDiscribeTxt().y=_scrollTxtRect.y;
//			this.getCastleDiscribeTxt().scrollRect=n
			
//			selectRace(0);
		}
		
		private function playCastleUI(overCB:Function):void
		{
			_container.addChild(_castleSelectUI);
			
			_castleSelectUI.gotoAndPlay(1);
			
//			var totalFrames:int	=	_castleSelectUI.totalFrames;
//			
//			_castleSelectUI.addEventListener(Event.ENTER_FRAME,function(e:Event):void
//			{
//				if(_castleSelectUI.currentFrame	==	totalFrames)
//				{
//					EventDispatcher(e.currentTarget).removeEventListener(Event.ENTER_FRAME,arguments.callee);
//					_castleSelectUI.stop();
//					overCB();
//				};
//			});
			
			_castleSelectUI.overCB	=	overCB;
		}
		
		private function cgCompleteHandler(val:MovieClip,pList:Array):void
		{
			LoadingLogic.getInstance().setProcess(100);
			StageInfo.systemSpace.addChild(val);
			val.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
		}
		
		private function onLoadingHandle(event:Event):void
		{
			if(event.type == ProgressEvent.PROGRESS)
			{
				var e:ProgressEvent	=	event as ProgressEvent;
				LoadingLogic.getInstance().setProcess(e.bytesLoaded*100/e.bytesTotal);
			}
		}			
		
		
		private function enterFrameHandler(e:Event):void
		{
			var target:MovieClip=MovieClip(e.currentTarget)
			var child:MovieClip=target.getChildAt(0)	as MovieClip;
			
			if(child.currentFrame	==	child.totalFrames)
			{
				target.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
				target.parent.removeChild(target);
				playCastleUI(init);
			}
		}
		
		
		public function hide():void
		{
			if(_castleSelectUI.parent)
				_castleSelectUI.parent.removeChild(_castleSelectUI);
			
			//=========初始化种族Icon
			var raceIcon:MovieClip;
			for(var i:uint	=0;	i<_raceNums;	i++)
			{
				raceIcon=this.getRaceIcon(i);
				raceIcon.removeEventListener(MouseEvent.CLICK,raceIconClickHandler);
			}
			//=========初始化城堡Icon
			var castleIcon:MovieClip;
			for(var j:uint=0;	j<_castleNums;	j++)
			{
				castleIcon=this.getCastleIcon(j);
				castleIcon.removeEventListener(MouseEvent.CLICK,castleIconClickHandler);
			}
			
//			if(_nextBtn)
//			{
			_nextBtn.removeEventListener(MouseEvent.CLICK,nextBtnClickHandler);
			_nextBtn.removeEventListener(MouseEvent.MOUSE_OVER,nextBtnOverHandler);
			_nextBtn.removeEventListener(MouseEvent.MOUSE_OUT,nextBtnOutHandler);
//			}
			
//			_nxTimer.stop();
			_scrollAnimation.stop();
		}
		
		public function recycle():void
		{
		}
		
		public function set completeCallBack(cb:Function):void
		{
			_completeCallBack=cb;
		}
		
		private function nextBtnClickHandler(e:MouseEvent):void
		{
			if(_selectedCastleId<0	||	_selectedRaceId<0)
			{
//				LoginAlert.showAlert(StaticIni.getSwfLangStr("UIStr_1001114"));
				return;
			}
			
			var obj:Object=new Object();
			obj.target=this;
			obj.raceId=this._selectedRaceId;
			obj.castleId=this._selectedCastleId;
			
			if(null		!=	_completeCallBack)
			{
				_completeCallBack(obj);
			}
			
			ActSee.PostLoginStatus(8997, 1, 0);
		}
		
		private function castleIconClickHandler(e:MouseEvent):void
		{	
			var castleIcon:MovieClip=e.currentTarget as MovieClip;
			
			if(false	==	castleIcon.buttonMode)	return;
			
			var id:int=this.getCastleIdFromMc(castleIcon);
			this.selectCastle(id);
		}
		
		private function raceIconClickHandler(e:MouseEvent):void
		{	
			var raceIcon:MovieClip=e.currentTarget as MovieClip;
			if(false	==	raceIcon.buttonMode	)	return;
			
			var id:int=this.getRaceIdFromMc(raceIcon);
			this.selectRace(id);
		}
		
		private function nextBtnOverHandler(e:MouseEvent):void
		{
			MovieClip(e.currentTarget).gotoAndStop(1);
		}
		
		private function nextBtnOutHandler(e:MouseEvent):void
		{
			MovieClip(e.currentTarget).gotoAndStop(2);
		}
		
//		private function castleTxtOverHandler(e:MouseEvent):void
//		{
//			_nxTimer.stop();
//		}
		
//		private function castleTxtOutHandler(e:MouseEvent):void
//		{
//			_nxTimer.timingCall(this.scrollDiscribeInfo,null,700);
//		}
		
		private function selectRace(raceId:int):void
		{
			_selectedRaceId=raceId;
			_selectedCastleId	=	-1;
			
			resetRaceIcons();
			resetRaceTxt();
//			resetMask(raceId);
			resetCastleIcons(raceId);
			
			//重置城堡描述文字
			getCastleDiscribeTxt().scrollRect	=	null;
			getCastleDiscribeTxt().htmlText	=	_defaultCastleDis;
			_scrollAnimation.stop();

			///
			
			var raceIcon:MovieClip=getRaceIcon(raceId);
			raceIcon.gotoAndStop(1);
			raceIcon.buttonMode=false;
			
			
			
			this.getRaceDiscribeTxt(raceId).scrollRect	=	null;
			this.getRaceDiscribeTxt(raceId).htmlText=getRaceDiscribe(raceId);
			
			
			
			
//			this.getCastleDiscribeTxt().scrollV=1;
			
//			_scrollAnimation.reset();
			//
//			_castleSelectUI.castleSheet.y=620;
			
			TweenLite.to(_castleSelectUI.castleSheet,0.3,{y:620,onComplete:function():void
					{
						TweenLite.to(_castleSelectUI.castleSheet,0.3,{y:400});
						
						selectCastle(10);
						
						if(false	==	_isCastleTipsShowed)
						{
							_selectCastleMc.gotoAndPlay(2);
							_isCastleTipsShowed	=	true;
						}
					}
				}
			);
			
//			//默认选择第一个
//			selectCastle(getFirstCastleByRace(raceId));
			
//			var firstCastleId:int=this.getFirstCastleByRace(raceId);
//			var castleIcon:MovieClip=this.getCastleIcon(firstCastleId);
//			var castle:MovieClip=this.getCastle(firstCastleId);
//			
//			castleIcon.gotoAndStop(1);
//			castleIcon.buttonMode=false;
//			
//			castle.gotoAndStop(1);
//			
//			//			this.getRaceDiscribeTxt().scrollV=1;
//			
//			this.getCastleDiscribeTxt().autoSize	=	TextFieldAutoSize.LEFT;
//			this.getCastleDiscribeTxt().htmlText=this.getCastleDiscribe(firstCastleId);
//			
//			var hei:Number	=	this.getCastleDiscribeTxt().textHeight;
//			
//			this.getCastleDiscribeTxt().autoSize=TextFieldAutoSize.NONE;
//			this.getCastleDiscribeTxt().height	=	hei	+ 10;//防自动滚轮
//			
//			_scrollAnimation.start(getCastleDiscribeTxt(),new Rectangle(0,0,_scrollTxtRect.width,_scrollTxtRect.height));
//			_selectedCastleId=firstCastleId;
		}
	
		private function scrollDiscribeInfo():void
		{
//			if(this.getRaceDiscribeTxt())
//				this.getRaceDiscribeTxt().scrollV	+=	1;
			
			if(this.getCastleDiscribeTxt())
			{
				var rect:Rectangle=this.getCastleDiscribeTxt().scrollRect;
				
				rect.y	+=1;
			}
			
//			_txtNum+=1;
			
//			this.getCastleDiscribeTxt().autoSize=TextFieldAutoSize.LEFT;
//			this.getCastleDiscribeTxt().scrollRect=	new Rectangle(0,
//															_txtNum,
//															this.getCastleDiscribeTxt().width,
//															80
//													)
//				new Rectangle(100,
//								100,
//								this.getCastleDiscribeTxt().width,
//								this.getCastleDiscribeTxt().height
//							)
//			);
			
		}
		
		private function selectCastle(castleId:int):void
		{
			var castleIcon:MovieClip;
			var castle:MovieClip;
			
			//重置一下状态，但是否显示不改变
			for(var i:uint=0;	i<_castleNums;	i++)
			{
				castleIcon=this.getCastleIcon(i);
				castleIcon.buttonMode=true;
				castleIcon.gotoAndStop(2);
				
//				castle=this.getCastle(i);
//				castle.gotoAndStop(2);
			}
			
			castleIcon=this.getCastleIcon(castleId);
//			castle=this.getCastle(castleId);
			
			castleIcon.gotoAndStop(1);
			castleIcon.buttonMode=false;
			
//			castle.gotoAndPlay(1);
			
			this.getCastleDiscribeTxt().autoSize	=	TextFieldAutoSize.LEFT;
			this.getCastleDiscribeTxt().htmlText	=	this.getCastleDiscribe(castleId);
			
			var hei:Number	=	this.getCastleDiscribeTxt().textHeight;
			
			this.getCastleDiscribeTxt().autoSize=TextFieldAutoSize.NONE;
			this.getCastleDiscribeTxt().height	=	hei	+ 10;//防自动滚轮
			
			_scrollAnimation.start(getCastleDiscribeTxt(),new Rectangle(0,0,_scrollTxtRect.width,_scrollTxtRect.height));
//			this.getCastleDiscribeTxt().scrollV	=1;
			
//			_scrollAnimation.reset();
			
			_selectedCastleId=castleId;
		}
		
		/***重设种族按钮*/
		private function resetRaceIcons():void
		{
			var raceIcon:MovieClip;
			var raceDiscribeIndex:String;
			var langStr:String;
			
			for(var i:uint=0;	i<_raceNums;	i++)
			{
				raceIcon=this.getRaceIcon(i);
				raceIcon.gotoAndStop(2);
				raceIcon.buttonMode=true;
			}
		}
		private function resetCastleOnMap():void
		{
			
		}
		
		/***重设城堡状态，地图上的城堡图标，与可选的城堡Icon*/
		private function resetCastleIcons(raceId:int	=	-1):void
		{		
			var castleIcon:MovieClip;
//			var castle:MovieClip;
			
			for(var i:uint=0;	i<_castleNums	;i++)
			{
				castleIcon=this.getCastleIcon(i);
//				castle	=	this.getCastle(i);
				
				castleIcon.gotoAndStop(2);
				castleIcon.visible=false;
				castleIcon.buttonMode=true;
				
//				castle.gotoAndStop(2);
//				castle.buttonMode=false;
			}
			
			//如果没有选择种族，只是把所有城堡都停在初始状态
			if(	raceId	<	0	)	return;
			
			
			//根据种族显示需要的城堡按钮
			var castleIconBg:MovieClip=getCastleBg();
//			var visibleCastleIconList:Array=[];
			
			var castleArr:Array=String(_castleList[raceId]).split(",");
			var len:int	=	castleArr.length;
			for(var j:uint=0;	j<len;	j++)
			{
				castleIcon=getCastleIcon(castleArr[j]);
				castleIcon.visible=true
//				visibleCastleIconList.push(castleIcon);
			}
//			averageArrange(visibleCastleIconList,castleIconBg);
		}
		private function resetRaceTxt():void
		{
			getRaceDiscribeTxt(0).htmlText=_defaultRace0Dis;
			getRaceDiscribeTxt(3).htmlText=_defaultRace1Dis;
		}
		
//		/**
//		 * 重置大陆上的城堡遮罩 
//		 * @param raceId 当前选择的种族
//		 * 
//		 */		
//		private function resetMask(raceId:int):void
//		{			
//			if(raceId<2)//光明方
//			{
//				getCampMaskMc(0).gotoAndStop(1);
//				
//				if(1	==	getCampMaskMc(1).currentFrame)//如果已经不可选了，就不播了
//				{
//					getCampMaskMc(1).play();
//				}
//			}
//			else
//			{
//				getCampMaskMc(1).gotoAndStop(1);
//				
//				if(1	==	getCampMaskMc(0).currentFrame)
//				{
//					getCampMaskMc(0).play();
//				}
//			}
//		}
		
		//========
		private function getAnimationURL():String
		{
			return "res/prototype/castleSelectAnimation.swf";
		}
		
		private function getFirstCastleByRace(raceId:int):int
		{
			var castleStr:String=_castleList[raceId];
			var castleList:Array=	castleStr.split(",");
			
			if(castleList.length>0)
			{
				var firstId:int=castleList[0];
				return firstId;
			}
			return -1;
		}
		
		//==================================================================================

		/**
		 * 获取【用来选择】的城堡 元件
		 * @param castleId
		 * @return 
		 * 
		 */		
		private function getCastleIcon(castleId:int):MovieClip
		{
			return _castleSelectUI.castleSheet["icon_castle"+castleId];
		}
		
//		/**
//		 * 获取地图上的城堡元件 
//		 * @param castleId
//		 * @return 
//		 * 
//		 */		
//		private function getCastle(castleId:int):MovieClip
//		{
//			return _castleSelectUI["Castle_"+castleId];
//		}
		
		/**
		 * 获取种族元件 
		 * @param raceId
		 * @return 
		 * 
		 */		
		private function getRaceIcon(raceId:int):MovieClip
		{
			return _castleSelectUI["zz_"+raceId];
		}
		
		/**
		 *  
		 * @return 
		 * 
		 */		
		private function getCastleBg():MovieClip
		{
			return 	_castleSelectUI.getChildByName("castleIconContainer") as MovieClip;
		}
		
		
		/**
		 * 获取种族描述文字 
		 * @param raceId
		 * @return 
		 * 
		 */		
		private function getRaceDiscribe(raceId:int):String
		{
			var raceDiscribeIndex:String="zzinfo"+raceId;
			var langStr:String=StaticIni.getIniVar("GameLoginConfig",raceDiscribeIndex);
			
			return StaticIni.getSwfLangStr(langStr);
		}
		
		/**
		 * 获取城堡描述 
		 * @param castleId
		 * @return 
		 * 
		 */		
		private function getCastleDiscribe(castleId:int):String
		{
			var castleDiscribeObj:Object=StaticIni.getIniObj("Castle_"+castleId);
			//显示对应文本
			var langStr:String	=	StaticIni.getSwfLangStr(castleDiscribeObj["Tips"]);	
			
			return	langStr;
		}
		
		/**
		 * 获取城堡总数 
		 * @return 
		 * 
		 */		
		private function getCastleNums():int
		{
			return int(StaticIni.getIniVar("GameRaceConfig","castleNums"));
		}
		
		/**
		 * 获取种族总数 
		 * @return 
		 * 
		 */		
		private function getRaceNums():int
		{
			return int(StaticIni.getIniVar("GameRaceConfig","raceNums"));
		}
		
		/**
		 * 获取种族可选的城堡 
		 * @param raceId
		 * @return 
		 * 
		 */		
		private function getRaceCastles(raceId:int):String
		{
			return StaticIni.getIniVar("GameRaceConfig","race"+raceId+"_castle")
		}
		
		/**
		 * 获取种族描述文本框
		 * @return 
		 * 
		 */		
		private function getRaceDiscribeTxt(raceId:int):TextField
		{
//			TextField(_castleSelectUI.zz_txt0).htmlText=_defaultRace0Dis;
//			TextField(_castleSelectUI.zz_txt1).htmlText=_defaultRace1Dis;
			
			if(raceId<2)
			{
				return 	TextField(_castleSelectUI.zz_txt0);
			}
			else
			{
				return 	TextField(_castleSelectUI.zz_txt1);
			}
		}
		
		/**
		 * 获取城堡描述文本框 
		 * @return 
		 * 
		 */		
		private function getCastleDiscribeTxt():TextField
		{
			return TextField(_castleSelectUI.castleSheet.caslteDiscribeTxt);
		}
		
		/**
		 * 从元件获取种族ID 
		 * @param raceIcon
		 * @return 
		 * 
		 */		
		private function getRaceIdFromMc(raceIcon:MovieClip):int
		{
			var name:String=raceIcon.name;
			var id:int	=	int(name.replace("zz_",""));
			return id;
		}
		
		/**
		 * 从城堡元件获取城堡ID//此ID为0-9 
		 * @param castleIcon
		 * @return 
		 * 
		 */		
		private function getCastleIdFromMc(castleIcon:MovieClip):int
		{
			var name:String=castleIcon.name;
			
			var id:int	=	int(name.replace("icon_castle",""));
			return id;
		}
		
//		/**
//		 *获取阵营遮罩元件 
//		 */		
//		private function getCampMaskMc(campId:int):MovieClip
//		{
//			if(0	==	campId)
//			{
//				return _castleSelectUI.race0Mask;
//			}
//			else
//			{
//				return _castleSelectUI.race1Mask;
//			}
//		}
		
		/**
		 * 将一组显示对象排列在指定的背景上,使用时保证所有显示对象在同一级
		 * @param targetList
		 * @param bg
		 */		
		private function averageArrange(targetList:Array,bg:MovieClip):void
		{
			var len:uint=targetList.length;
			
			var gapX:Number;
			var totalWidth:Number=0;
			for(var i:uint=0;	i<len;	i++)
			{
				totalWidth+=	targetList[i].width;
			}
			gapX=(bg.width-totalWidth)/(len-1);
			
			var offsetX:Number=0;
			for(var j:uint=0;	j<len	;j++)
			{
				targetList[j].x=bg.x+offsetX;
				targetList[j].y=bg.y+(bg.height-targetList[j].height)/2;
				
				offsetX+=targetList[j].width+gapX;
			}
		}
		
	}
}